package com.radiation.rms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.radiation.rms.common.R;
import com.radiation.rms.common.RMSCommon;
import com.radiation.rms.common.RMSConstant;
import com.radiation.rms.entity.AbnormalData;
import com.radiation.rms.entity.RealTimeData;
import com.radiation.rms.mapper.AbnormalDataMapper;
import com.radiation.rms.service.AbnormalDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.radiation.rms.service.AreaService;
import com.radiation.rms.vo.RealDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 异常数据表 服务实现类
 * </p>
 *
 * @author 1062
 * @since 2021-04-02
 */
@Service
public class AbnormalDataServiceImpl extends ServiceImpl<AbnormalDataMapper, AbnormalData> implements AbnormalDataService {
    @Autowired
    private AreaService areaService;
    @Autowired
    private RMSCommon rmsCommon;
    @Override
    public R getAbnormalData(RealDataVo realDataVo) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<String> departmentIds = rmsCommon.getDepartmentIds(username);
        if (departmentIds == null || departmentIds.size() == 0) {
            return R.error().message(RMSConstant.PERMISSION_DENIED);
        }
        realDataVo.setDepartmentIds(departmentIds);
        PageHelper.startPage(realDataVo.getCurrentPage(), realDataVo.getPageSize());
        List<AbnormalData> abnormalDataList = this.baseMapper.getAbnormalData(realDataVo);
        List<RealDataVo> collect = abnormalDataList.stream().map(abnormalData -> {
            abnormalData.setFullName(areaService.getPath(abnormalData.getDepartmentId()));
            return realDataVo;
        }).collect(Collectors.toList());
        PageInfo<AbnormalData> abnormalDataPageInfo = new PageInfo<>(abnormalDataList);
        return R.ok().data("items", abnormalDataList).data("total", abnormalDataPageInfo.getTotal());
    }
}
